﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Data.Linq;
using System.Linq.Expressions;
namespace BAL
{
    public class Paper_Ques : BaseBAL<Paper_QueInfo, Models.Paper_Ques>
    {
        public static int GetMaxOrderIdValue(int examPagerId,int subPolicyId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                int? max = (from item in db.GetTable<Models.Paper_Ques>()
                             where item.SubPolicyId == subPolicyId && item.ExamPaperId == examPagerId
                             && item.OrderId.HasValue
                             select item.OrderId).Max();
                return max.HasValue ? max.Value : 0;
            }
        }

        internal static Paper_QueInfo TransQuestion2QuestionInfo(Models.Paper_Ques paper_Ques)
        {
            Paper_QueInfo paper_QueInfo = new Paper_QueInfo();

            Utility.SetObjectValueWithTheSamePropertyName(paper_QueInfo, paper_Ques);

            return paper_QueInfo;
        }
        public static void SavePaper_QueInfoOrderId(int ExamPaperId, int questionId, int orderId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<Paper_QueInfo> list = db.GetTable<Models.Paper_Ques>().Where(t => t.ExamPaperId == ExamPaperId && t.QuesId == questionId).Select(c => TransQuestion2QuestionInfo(c)).ToList();
                for (int i = 0; i < list.Count(); i++)
                {
                    list[i].OrderId = orderId;
                    Edit(list[i]);
                }
            }
        }

        public static int GetQuesInfoOrderIdByExamPagerId(int examPagerId, int questionInfoId)
        {
            try
            {
                using (var db = DatabaseHelper.GetDashboardData())
                {
                    var query = db.GetTable<Models.Paper_Ques>().Where(p => p.ExamPaperId == examPagerId && p.QuesId == questionInfoId).Select(p => p.OrderId);
                    foreach (var item in query)
                    {
                        if (item.HasValue)
                        {
                            return item.Value;
                        }
                    }
                    return 0;
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        public static void DeletePaper_QuesInfoByExamPagerId(int examPagerId)
        {
            try
            {
                using (var db = DatabaseHelper.GetDashboardData())
                {
                    IList<Paper_QueInfo> tInfos = db.GetTable<Models.Paper_Ques>().Where(p => p.ExamPaperId == examPagerId).Select(t => TransT2ToT1(t)).ToList<Paper_QueInfo>();
                    foreach (var item in tInfos)
                    {
                        Delete(item);
                    }
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 取得试题Id列表
        /// </summary>
        /// <param name="ExamPaperId"></param>
        /// <returns></returns>
        public static IList<int> GetQuesId(int ExamPaperId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<int> ids = db.GetTable<Models.Paper_Ques>().Where(t => t.ExamPaperId == ExamPaperId).Select(t => t.QuesId).ToList();
                return ids;
            }
        }
        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="examPaperId"></param>
        /// <param name="QuesId"></param>
        public static void Del(int examPaperId, int QuesId)
        {
            IList<Expression<Func<Models.Paper_Ques, bool>>> funcs = new List<Expression<Func<Models.Paper_Ques, bool>>>();
            if (examPaperId > 0)
            {
                funcs.Add(t => t.ExamPaperId == examPaperId);
            }
            if (QuesId > 0)
            {
                funcs.Add(t => t.QuesId == QuesId);
            }
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<int> ids = GetT2IQueryable(funcs, db).Select(t => t.Id).ToList();
                if (ids.Count > 0)
                {
                    Delete(ids);
                }
            }
        }
    }
}

